package com.charles.recordapp;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static com.charles.recordapp.R.*;

public class HomeFragment extends Fragment {
    private Button vTopAcion;
    private Button hTopAcion;
    private View whitepoint;

    private Button pauseAction;
    private Button endAction;
    private Button startAction;
    private Button startActions;
    private boolean buttonSelected = false;
    private ImageView menuCircleBg;

    //图片资源
    private int[] res = {R.id.id_a, R.id.id_b, R.id.id_c, R.id.id_d, R.id.id_e, R.id.id_f};
    //存放ImageView
    private final ArrayList<Button> menuList = new ArrayList<Button>();
    //菜单是不是展开
    private boolean isNotExpand = true;

    private Button format480;
    private Button format720;
    private Button format1080;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(layout.home_fragment, container, false);
        view.canScrollVertically(1);

        vTopAcion = view.findViewById(id.vTopAcion);
        vTopAcion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButtonAction(vTopAcion);
            }
        });
        hTopAcion = view.findViewById(id.hTopAcion);
        hTopAcion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButtonAction(hTopAcion);
            }
        });
        whitepoint = view.findViewById(id.home_icon_point);

        startAction = view.findViewById(id.startAction);
        startAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRecordAction(startAction);
            }
        });
        startActions = view.findViewById(id.startActions);
        startActions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRecordAction(startActions);
            }
        });


        pauseAction = view.findViewById(id.home_btn_pause);
        pauseAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRecordAction(pauseAction);
            }
        });

        endAction = view.findViewById(id.home_btn_end);
        endAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRecordAction(endAction);
            }
        });

        //利用循环，初始化ImageView组件
        for (int i = 0; i < res.length; i++) {
            Button button = (Button) view.findViewById(res[i]);
            //存放在list中
            menuList.add(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickMenuAction(button);
                }
            });
        }
        menuCircleBg = view.findViewById(id.menu_circle_bg);

        //视频尺寸选择
        format480 = view.findViewById(id.format480);
        format480.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFormatAction(format480);
            }
        });

        format720 = view.findViewById(id.format720);
        format720.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFormatAction(format720);
            }
        });

        format1080 = view.findViewById(id.format1080);
        format1080.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFormatAction(format1080);
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    public void setButtonAction(Button button) {
        vTopAcion.setTextSize(button == vTopAcion ? 18 : 14);
        vTopAcion.setTypeface(null, Typeface.BOLD);
        hTopAcion.setTextSize(button == hTopAcion ? 18 : 14);
        hTopAcion.setTypeface(null, Typeface.BOLD);
        button.setTypeface(null, Typeface.BOLD);
        //取控件当前的布局参数并调整
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) whitepoint.getLayoutParams();
        params.startToStart = button.getId();//((ConstraintLayout.LayoutParams) button.getLayoutParams()).getLayoutDirection();
        params.endToEnd = button.getId();//((ConstraintLayout.LayoutParams) button.getLayoutParams()).getLayoutDirection();
        //使设置好的布局参数应用到控件
        whitepoint.setLayoutParams(params);
    }

    //点击事件
    public void clickMenuAction(View view) {
        switch (view.getId()) {
            //主菜单被点击
            case R.id.id_a:
                //主菜单没有展开时被点击
                if (isNotExpand == true) {
                    //启动动画
                    startAnim();
                } else {
                    //关闭动画
                    closeAnim();
                }
                break;
            //定义其他组件被点击时触发的事件
            default:
                Toast.makeText(view.getContext(), "您点击了:" + view.getId(), Toast.LENGTH_LONG).show();
                break;
        }
    }

    //开始动画
    private void startAnim() {
        menuCircleBg.setVisibility(VISIBLE);
        //遍历第一个不是主菜单的ImageView列表
        for (int i = 1; i < res.length; i++) {
            //获取展开角度
            float angle = (285 * 1.0f / (res.length - 2)) * (i - 1);
            //获取X位移
            PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("translationX", 0, (float) (Math.sin((angle / -90)) * 130));
            //获取Y位移
            PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("translationY", 0, (float) (Math.cos((angle / -90)) * 130));
            //设置ImageView的属性动画
            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(menuList.get(i), holder1, holder2);
            // ObjectAnimator animator =
            // ObjectAnimator.ofFloat(menuList.get(i), "translationY", 0, i *
            // 60);
            //动画时间
            animator.setDuration(1000);
            //动画延迟时间
            animator.setFrameDelay(500 * i);
            //设置加速器
            animator.setInterpolator(new BounceInterpolator());
            //启动动画
            animator.start();
            isNotExpand = false;
        }
    }

    //关闭动画
    private void closeAnim() {
        menuCircleBg.setVisibility(INVISIBLE);
        for (int i = 1; i < res.length; i++) {
            float angle = (285 * 1.0f / (res.length - 2)) * (i - 1);
            PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("translationX", (float) (Math.sin((angle / -90)) * 130), 0);
            PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("translationY", (float) (Math.cos((angle / -90)) * 130), 0);
            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(menuList.get(i), holder1, holder2);
            // ObjectAnimator animator =
            // ObjectAnimator.ofFloat(menuList.get(i), "translationY", i * 60,
            // 0);
            animator.setDuration(300);
            animator.start();
            isNotExpand = true;
        }
    }

    //选择画质事件处理
    public void setFormatAction(Button button) {
        format480.setBackgroundResource(0);
        format480.setTextColor(getResources().getColor(color.maincolor, null));
        format720.setBackgroundResource(0);
        format720.setTextColor(getResources().getColor(color.maincolor, null));
        format1080.setBackgroundResource(0);
        format1080.setTextColor(getResources().getColor(color.maincolor, null));
        button.setBackgroundResource(mipmap.home_btn_selected);
        button.setTextColor(getResources().getColor(color.white, null));
    }

    //开始结束和暂停按钮控件
    @SuppressLint("ResourceAsColor")
    public void setRecordAction(Button button) {
        if (button == startAction || button == startActions || button == endAction) {
            int isVisible = (pauseAction.getVisibility() == INVISIBLE) ? VISIBLE : INVISIBLE;
            pauseAction.setVisibility(isVisible);
            endAction.setVisibility(isVisible);
            //利用循环，展开菜单时隐藏环形menu相关组件
            for (int i = 0; i < menuList.size(); i++) {
                Button menu = (Button) menuList.get(i);
                menu.setVisibility(isVisible == INVISIBLE ? VISIBLE : INVISIBLE);
                if (i == 1 && !isNotExpand) {
                    menuCircleBg.setVisibility(menu.getVisibility());
                }
            }

            if (button == startAction || button == startActions) {
                //点击开始录制视频按钮
                startAction.setVisibility(VISIBLE);
                startActions.setVisibility(VISIBLE);
                button.setVisibility(INVISIBLE);
            } else {
                //点击结束录制视频按钮
                startAction.setVisibility(VISIBLE);
                startActions.setVisibility(INVISIBLE);
            }
        }

        if (button == pauseAction) {
            //点击暂停按钮

        }
    }
}